package com.family.gmall.list.receiver;

import com.family.gmall.common.constant.MqConst;
import com.family.gmall.list.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * @Auther: family
 * @Date: 2023/8/6 18:36
 * @Description:
 */
@Component
@Configuration
public class ProductReceiver {

    @Resource
    private SearchService searchService;


    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            // durable持久化配置
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_UPPER}))
    public void upperGoodsToEs(Long skuId, Message message, Channel channel) {
        try {
            if (skuId != null) {
                searchService.upperGoods(skuId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //确认消费者消费信息！
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            // durable持久化配置
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = {MqConst.ROUTING_GOODS_LOWER}))
    public void lowerGoodsToEs(Long skuId, Message message, Channel channel) {
        try {
            if (skuId != null) {
                searchService.lowerGoods(skuId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //消息确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
